<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var countAndSay = function (n) {
      let str = "1";
      for (let i = 2; i <= n; ++i) {
        const sb = [];
        let start = 0;
        let pos = 0;

        while (pos < str.length) {
          while (pos < str.length && str[pos] === str[start]) {
            pos++;
          }
          sb.push('' + (pos - start) + str[start]);
          start = pos;
        }
        str = sb.join('');
      }

      return str;
    };
    console.log(countAndSay(4));
  </script>
</body>

</html>